﻿@using FwProject.Settlement.Dto
@using FwProject.PrePaymentInfo.Dto
@using FwProject.PrePaymentInfo
@model Tuple<ReceiptInfoDto, List<SettlementInfoDto>>
@{
    ViewBag.Title = "收款单[" + Model.Item1.ReceiptInfoNumber + "]";
}
@section styles{
    <link href="~/Theme/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="~/Theme/css/plugins/datapicker/datepicker3.css" rel="stylesheet">
    <style type="text/css">
        .input-group[class*=col-] {
            padding: 0 15px;
        }

        .checked {
            position: absolute;
            top: 3px;
            left: 220px;
            height: 87px;
            width: 141px;
            background: url('/images/checked.gif') left top no-repeat;
            z-index: 9999;
        }
    </style>
}
<div class="row wrapper wrapper-content animated fadeInRight">
    <div class="col-sm-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>@ViewBag.Title</h5>
                <div class="ibox-tools">
                    @*<a class="btn btn-primary btn-xs" onclick="window.open('/Payment/PaymentPrint/@Model.Item1.PaymentinfoId')"> 打印</a>*@

                    @if (Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.未提交 || Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.退回)
                    {
                        <button class="btn btn-primary btn-xs" id="btnSave">保存</button>
                        <button class="btn btn-primary btn-xs" id="btnSubmitSave">提交</button>
                        @*<a class="btn btn-primary btn-xs" href="/Receipt/ReceiptSubmit?id=@Model.Item1.Id">提交</a>*@
                        <a class="btn btn-primary btn-xs" href="/Receipt/ReceiptAnTiExamine?id=@Model.Item1.Id">作废</a>
                    }
                    else if (Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.已提交)
                    {
                        if (IsGranted(FwProject.Authorization.PermissionNames.Receipt_Examine))
                        {
                            <a class="btn btn-primary btn-xs" href="/Receipt/ReceiptExamine?id=@Model.Item1.Id">审核</a>
                        }
                        else
                        {
                            <button class="btn btn-default btn-xs" type="button">无审核权限</button>
                        }
                        <a class="btn btn-primary btn-xs" onclick="window.open('/Print/ReceiptPrint?id=@Model.Item1.Id')">打印</a>
                    }
                    else
                    {
                        if (IsGranted(FwProject.Authorization.PermissionNames.Receipt_AnTiExamine))
                        {
                        <a class="btn btn-primary btn-xs" href="/Receipt/ReceiptAntiaudit?id=@Model.Item1.Id">反审核</a>
                        }
                        else
                        {
                            <button class="btn btn-default btn-xs" type="button">无反审核权限</button>
                        }
                        
                        <a class="btn btn-primary btn-xs" onclick="window.open('/Print/ReceiptPrint?id=@Model.Item1.Id')">打印</a>
                    }




                </div>
            </div>
            <div class="ibox-content" style="height:700px">
                <div class="row m-b-sm m-t-sm">
                    <form class="form-horizontal" id="form" action="/Receipt/Save?id=@Model.Item1.Id" data-ajax="true" data-ajax-update="#data_list" data-ajax-method="Post" data-ajax-mode="replace" data-ajax-success="ReloadAjaxCallbackgoback">
                        <input type="hidden" name="ReceiptInfoStatus" id="ReceiptInfoStatus" value="@Model.Item1.ReceiptInfoStatus" />
                        <div class="form-group">
                            <div class="col-md-4">
                                <label class="control-label col-md-3">单号：</label>
                                <div class="input-group">
                                    <input type="text" name="ReceiptInfoNumber" class="input-sm form-control" value="@Model.Item1.ReceiptInfoNumber" readonly="readonly">
                                </div>
                                @if (Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.已审核)
                                {
                                    <div class="checked" id="check_icon"></div>
                                }
                            </div>
                            <div class="col-md-4">
                                <label class="control-label col-md-3">收款日期：</label>
                                <div class="input-group">
                                    <input type="datetime" name="Dateofreceipt" class="input-sm form-control" value="@Model.Item1.Dateofreceipt.Value.ToString("yyyy-MM-dd HH:mm:ss")" readonly="readonly">
                                </div>
                            </div>
                            @if (Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.未提交 || Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.退回)
                            {
                                <div class="col-md-4">
                                    <label class="control-label col-md-3">经办人：</label>
                                    <div class="input-group" style="width:162px">
                                        <input type="hidden" name="AgentPeoperUserId" id="AgentPeoperUserId" value="@Model.Item1.AgentPeoperUserId" />
                                        <input type="text" id="dropdown-agentpeoperuserid" value="@Model.Item1.AgentPeoper" class="form-control" onblur="blurEmpty()">
                                        <div class="input-group-btn">
                                            <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown" style="line-height:1.455;padding:6px 12px;">
                                                <span class="caret"></span>
                                            </button>
                                            <ul class="dropdown-menu dropdown-menu-right" role="menu" style="z-index: 9999;"></ul>
                                        </div>
                                    </div>
                                </div>
                            }
                            else
                            {
                                <div class="col-md-4">
                                    <label class="control-label col-md-3">经办人：</label>
                                    <div class="input-group">
                                        <input type="text" name="AgentPeoper" class="input-sm form-control" value="@Model.Item1.AgentPeoper" readonly="readonly">
                                    </div>
                                </div>
                            }

                        </div>
                        <div class="form-group">
                            <div class="col-md-4">
                                <label class="control-label col-md-3">餐厅：</label>
                                <div class="input-group">
                                    <input type="text" name="RestaurantName" class="input-sm form-control" value="@Model.Item1.RestaurantInfoModel.RestaurantName" readonly="readonly">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <label class="control-label col-md-3">预收款余额：</label>
                                <div class="input-group">
                                    <input type="text" name="UsePrePaymentSurplus" id="UsePrePaymentSurplus" class="input-sm form-control" value="@Model.Item1.UsePrePaymentSurplus" readonly="readonly">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <label class="control-label col-md-3">使用预收款：</label>
                                <div class="input-group">
                                    @if (Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.未提交 || Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.退回)
                                    {
                                        <input type="text" name="UsePrePayment" id="UsePrePayment" class="input-sm form-control" value="@Model.Item1.UsePrePayment" oninput="changePrice(this)">
                                    }
                                    else
                                    {
                                        <input type="text" name="UsePrePayment" class="input-sm form-control" value="@Model.Item1.UsePrePayment" readonly="readonly">

                                    }
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-4">
                                <label class="control-label col-md-3">收款账户：</label>
                                <div class="input-group">
                                    <input type="text" name="AccountName" class="input-sm form-control" value="@ViewBag.Account" readonly="readonly">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <label class="control-label col-md-3">收款金额：</label>
                                <div class="input-group">
                                    @if (Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.未提交 || Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.退回)
                                    {
                                        <input type="text" name="TaskBackAmount" id="Actualprice" class="input-sm form-control" oninput="changePrice(this)" value="@Model.Item1.TaskBackAmount">
                                        <span style="color:red">应收金额：(@Model.Item2.Sum(o => o.Actualprice))</span>
                                        <input type="hidden" id="SumActualprice" value="@Model.Item2.Sum(o => o.Actualprice)">
                                    }
                                    else
                                    {
                                        <input type="text" name="TaskBackAmount" class="input-sm form-control" value="@Model.Item1.TaskBackAmount" readonly="readonly">

                                    }
                                </div>
                            </div>
                            @*<div class="col-md-4">
                                    <label class="control-label col-md-3">本次收回预收款：</label>
                                    <div class="input-group">
                                        @if (Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.未提交 || Model.Item1.ReceiptInfoStatus == ReceiptInfoStatus.退回)
                                        {
                                            <input type="text" name="ReceivedPrePayment" id="ReceivedPrePayment" class="input-sm form-control" oninput="changePrice(this)" value="0.00">
                                        }
                                        else
                                        {
                                            <input type="text" name="ReceivedPrePayment" class="input-sm form-control" value="@((Model.Item1.TaskBackAmount-Model.Item1.ActualAmount)>0 ? (Model.Item1.TaskBackAmount-Model.Item1.ActualAmount):0)" readonly="readonly">

                                        }
                                    </div>
                                </div>*@

                            @*<div class="col-md-4">
                                    <label class="control-label col-md-3">优惠金额：</label>
                                    <div class="input-group">
                                        <input type="text" name="PreferentialAmount" id="PreferentialAmount" oninput="changePrice(this)" class="input-sm form-control" value="@Model.Item1.PreferentialAmount">
                                    </div>
                                </div>*@
                        </div>
                        <div class="form-group">
                            <div class="col-md-12">
                                <label class="control-label col-md-3" style="width:132px">备注：</label>
                                <div class="input-group">
                                    @*<input type="text" name="RemarksConten" class="input-sm form-control" value="@Model.Item1.RemarksConten" style="width:800px" readonly="readonly">*@
                                    <textarea name="RemarksConten" class="input-sm form-control" style="width:800px">@Model.Item1.RemarksConten</textarea>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <hr />
                <div id="data_list">
                    <div>
                        <table class="table table-bordered table-hover table-rowselected">
                            <thead>
                                <tr>
                                    <th>状态</th>
                                    <th>单据类型</th>
                                    <th>单据编号</th>
                                    <th>单据日期</th>
                                    <th>配送日期</th>
                                    <th>制单金额</th>
                                    <th>未收金额</th>
                                    <th>本次收款金额</th>
                                    <th>收款优惠</th>
                                    <th>预收款</th>
                                    <th>备注</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model.Item2)
                                {
                                    <tr>
                                        <td>@item.ToVoidStatus</td>
                                        <td>@item.BalanceWayEnum</td>
                                        <td><a href="javascript:top.window.openTab(5756,'收据[@item.CreationNumber]','/Receipt/SettlementDetails?id=@item.Id')">@item.CreationNumber</a></td>
                                        <td>@item.CreationTime</td>
                                        <td>@item.DeliveryDate</td>
                                        <td>@item.Totalprice</td>
                                        <td>@item.Actualprice</td>
                                        <td>@item.ReceivedPrice</td>
                                        <td>@item.ReceivedDiscount</td>
                                        <td>@item.UsePrePayment</td>
                                        <td>@item.RemarksConten</td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>
<div id="modal_ajax" class="modal inmodal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
</div>
@section scripts{
    <script src="~/Theme/js/content.min.js?v=1.0.0"></script>
    <script src="~/Theme/js/plugins/iCheck/icheck.min.js"></script>
    <script src="~/Theme/js/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="~/Theme/js/plugins/suggest/bootstrap-suggest2014.js"></script>
    <script src="~/js/DateComm.js"></script>
    <script>
        $(document).ready(function () {

            $.getJSON("/Deposit/GetAgentPeoperData").done(function (data) {
                CommonAjaxCallback(data, function () {
                    bindSuggest(data.result);
                }, null);
            }).fail(function () {
                layer.msg('连接超时，获取经办人失败！', {
                    icon: 2,
                    time: 2000
                });
            });

            $('#modal_ajax').on('show.bs.modal', function (e) {
                $(this).addClass('loading');
                var $button = $(e.relatedTarget);
                var url = $button.data('url');
                if (!!url) {
                    url = url + ((url.indexOf("?") > -1) ? "&r=" : "?r=") + Math.random();
                    $(this).load(url, function () {
                        $.validator.unobtrusive.parse(this); //$("#form_model_create").validate();
                        $("input[name='CarOld']").datepicker({ todayBtn: "linked", keyboardNavigation: !1, forceParse: !1, calendarWeeks: !0, autoclose: !0 });
                        $(".i-checks").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green", });
                        $(this).removeClass('loading');


                        $('.btnDel').on('click', function () {
                            $(this).parent().parent().remove();
                        });

                    });
                }
            });
            $('#modal_ajax').on('hidden.bs.modal', function (e) {
                $(this).empty();
            });

            $('#btnSave').click(function () {
                $('#form').submit();
            });
            $("#btnSubmitSave").click(function () {
                layer.confirm('是否提交审核?', { icon: 3, title: '收款单提示' }, function (index) {
                    //do something
                    $("#ReceiptInfoStatus").val(1);
                    $('#form').submit();
                    layer.close(index);
                });

            });
        });


        function changePrice(obj) {
            var UsePrePaymentSurplus = parseFloat($('#UsePrePaymentSurplus').val());//预收款余额
            var UsePrePayment = parseFloat($('#UsePrePayment').val());//使用预收款
            var TaskBackAmount = parseFloat($('#Actualprice').val());//收款金额
            var SumActualprice = parseFloat($('#SumActualprice').val());//收款金额

            //var PreferentialAmount = parseFloat($('#PreferentialAmount').val());//优惠金额

            if (UsePrePayment > UsePrePaymentSurplus) {
                $('#UsePrePayment').val(UsePrePaymentSurplus);
            }
            if (UsePrePayment < 0) {
                $('#UsePrePayment').val(0);
            }
            if (TaskBackAmount < 0) {
                $('#Actualprice').val(0);
            }
            if (UsePrePayment + TaskBackAmount/* + PreferentialAmount*/ > SumActualprice) {
                layer.msg('预收款+应收不能大于总应收！', {
                    icon: 2,
                    time: 2000
                });

                //if (UsePrePayment + PreferentialAmount > SumActualprice) {

                //    if (UsePrePayment > SumActualprice) {
                //        $('#UsePrePayment').val(SumActualprice);
                //        UsePrePayment = SumActualprice;

                //        $('#PreferentialAmount').val(0);
                //        PreferentialAmount = 0;

                //    } else {
                //        PreferentialAmount = SumActualprice - UsePrePayment;
                //        $('#PreferentialAmount').val(PreferentialAmount);
                //    }


                //}
                if (UsePrePayment > SumActualprice) {
                    $('#UsePrePayment').val(SumActualprice);
                    UsePrePayment = SumActualprice;
                }

                $('#Actualprice').val(SumActualprice - UsePrePayment /*- PreferentialAmount*/);


            }

        }
        function bindSuggest(data) {
            if ($("#dropdown-agentpeoperuserid") != undefined) {
                $role = $("#dropdown-agentpeoperuserid").bsSuggest({
                    effectiveFields: ["name"],
                    effectiveFieldsAlias: { "name": "姓名" },
                    searchFields: ["name"],
                    clearable: true,
                    getDataMethod: "data",
                    //url: "/Users/GetSuggestData?type=司机类型&keyword=",
                    data: { value: data },
                    idField: "id",
                    keyField: "name",
                }).on('onDataRequestSuccess', function (e, result) {
                    console.log('onDataRequestSuccess: ', result);
                }).on('onSetSelectValue', function (e, keyword, data) {
                    $("#AgentPeoperUserId").val(data.id);
                    console.log('onSetSelectValue: ', keyword, data);
                }).on('onUnsetSelectValue', function () {
                    console.log('onUnsetSelectValue');
                });
            }
        }
        function blurEmpty() {
            var cityObj = $("#dropdown-agentpeoperuserid");
            var DriverId = $("#AgentPeoperUserId");
            if (cityObj.val() == "") {
                DriverId.attr("value", "");
            }
        }

        function setPrice(obj) {
            var TotalPrice = $('#TotalPrice').val();
            var Actualprice = $('#Actualprice').val();
            var ReceivedDiscount = $('#ReceivedDiscount').val();
            if (ReceivedDiscount > TotalPrice) {
                $('#ReceivedDiscount').val(0);
            } else {
                $('#Actualprice').val(TotalPrice - ReceivedDiscount);
            }

        }

        function ReloadAjaxCallbackgoback(data, successInfo) {
            CommonAjaxCallback(data, function () {
                window.location.reload();
            }, successInfo);
        }

    </script>
}


